package com.aooci.ordercat.desktop;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.aooci.ordercat.desktop.view.DashboardController;
import com.aooci.ordercat.domain.ItemCategory;
import com.aooci.ordercat.enums.OrderStatus;
import com.aooci.ordercat.service.AccountingService;
import com.aooci.ordercat.service.DashboardService;
import com.aooci.ordercat.service.OrderOperationService;
import com.aooci.ordercat.service.PrintingService;
import com.aooci.ordercat.vo.ItemVo;


@SuppressWarnings("restriction")
public class OrdercatMain extends Application {
	
	private ClassPathXmlApplicationContext ctx;
	private DashboardService dashboardService;
	private OrderOperationService orderOperationService;
	private PrintingService printingService;
	private AccountingService accountingService;
	
    private Stage primaryStage;
    private VBox rootLayout;
    
    private List<ItemCategory> categories;
    private Map<Integer, List<ItemVo>> items;
    private Map<Integer, Button> diningHallTables = new HashMap<Integer, Button>(); 
    //private ListView<Takeaway> takeawayButtons;
    
    

	public OrdercatMain() {
		super();
		this.ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		// get the spring bean based on it's name
		DriverManagerDataSource dataSource = (DriverManagerDataSource)this.ctx.getBean("dataSource");
		//re-write the url based on a variable passed
		dataSource.setUrl("jdbc:h2:file:E:\\eclipse-standard-luna-R-win32-x86_64\\workspace\\ordercat-javafx\\database\\fdj");  
		
		this.dashboardService = (DashboardService)ctx.getBean("dashboardService");
		this.orderOperationService = (OrderOperationService)ctx.getBean("orderOperationService");
		this.printingService = (PrintingService)ctx.getBean("printingService");
		this.accountingService = (AccountingService)ctx.getBean("accountingService");
	}

	public static void main(String[] args) {
		Application.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		this.primaryStage = primaryStage;
		this.primaryStage.setTitle(SystemConstant.DASHBOARD_PANEL_TITLE);
        initRootLayout();
        initData();
	}
	
    public void initRootLayout() {
        try {	
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(OrdercatMain.class.getResource(SystemConstant.DASHBOARD_PANEL_FXML));
            this.rootLayout = (VBox) loader.load();
            DashboardController dashboardController = loader.getController();
            dashboardController.init(this);
            Scene scene = new Scene(this.rootLayout);
            scene.getStylesheets().add("css/desktop.css");
            this.primaryStage.setScene(scene);
            this.primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void initData(){
    	this.categories = this.dashboardService.loadItemCategories();
    	this.items = new HashMap<Integer, List<ItemVo>>();
//    	for(ItemCategory category:this.categories){
//    		this.items.put(category.getId(), this.orderOperationService.loadItemsByItemCategoryId(category.getId()));
//    	}
    	for(ItemVo item:this.orderOperationService.loadAllItems()){
    		if(this.items.get(item.getCategoryId()) == null){
    			List<ItemVo> items = new ArrayList<ItemVo>();
    			items.add(item);
    			this.items.put(item.getCategoryId(), items);
    		}else{
    			this.items.get(item.getCategoryId()).add(item);
    		}
    	}
    }

	public Stage getPrimaryStage() {
		return primaryStage;
	}

	public DashboardService getDashboardService() {
		return dashboardService;
	}

	public OrderOperationService getOrderOperationService() {
		return orderOperationService;
	}

	public PrintingService getPrintingService() {
		return printingService;
	}

	public AccountingService getAccountingService() {
		return accountingService;
	}

	public List<ItemCategory> getCategories() {
		return categories;
	}

	public Map<Integer, List<ItemVo>> getItems() {
		return items;
	}

	public Map<Integer, Button> getDiningHallTables() {
		return diningHallTables;
	}
}
